package org.gdstash.ui;

import org.gdstash.util.GDImagePool;
import org.gdstash.util.GDMsgFormatter;

import javax.swing.*;
import javax.swing.border.Border;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

public class GDLogoDialog extends JDialog {
    private Component owner;
    private JPanel panel;
    private JLabel lblImage;
    private JLabel lblIcon;
    private JLabel lblText;
    private JButton btnClose;

    public GDLogoDialog(final String text) {
        this(text, 0);
    }

    public GDLogoDialog(final String text, final int iconType) {
        this.owner = null;
        this.buildPanel(text, iconType);
    }

    public GDLogoDialog(final String text, final Frame owner) {
        this(text, 0, owner, "", false);
    }

    public GDLogoDialog(final String text, final int iconType, final Frame owner) {
        this(text, iconType, owner, "", false);
    }

    public GDLogoDialog(final String text, final Frame owner, final boolean modal) {
        this(text, 0, owner, "", modal);
    }

    public GDLogoDialog(final String text, final int iconType, final Frame owner, final boolean modal) {
        this(text, iconType, owner, "", modal);
    }

    public GDLogoDialog(final String text, final Frame owner, final String title) {
        this(text, 0, owner, title, false);
    }

    public GDLogoDialog(final String text, final int iconType, final Frame owner, final String title) {
        this(text, iconType, owner, title, false);
    }

    public GDLogoDialog(final String text, final int iconType, final Frame owner, final String title, final boolean modal) {
        super(owner, title, modal);
        this.owner = owner;
        this.buildPanel(text, iconType);
    }

    protected void buildPanel(final String text, final int iconType) {
        this.panel = new JPanel();
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        Font fntButton = UIManager.getDefaults().getFont("Button.font");
        if (fntButton == null) {
            fntButton = fntLabel;
        }
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        fntButton = fntButton.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        (this.lblImage = new JLabel()).setIcon(GDImagePool.iconProgress);
        this.lblImage.setBorder(compound);
        this.lblImage.setHorizontalAlignment(0);
        final JPanel pnlText = this.buildTextPanel(text, iconType);
        (this.btnClose = new JButton(GDMsgFormatter.getString(GDMsgFormatter.rbUI, "BTN_OK"))).setIcon(GDImagePool.iconBtnOk24);
        this.btnClose.setFont(fntButton);
        this.btnClose.addActionListener(new CloseActionListener());
        layout = new GroupLayout(this.panel);
        this.panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage).addComponent(pnlText).addComponent(this.btnClose));
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblImage)).addGroup(layout.createParallelGroup().addComponent(pnlText)).addGroup(layout.createParallelGroup().addComponent(this.btnClose));
        layout.setVerticalGroup(vGroup);
        layout.linkSize(0, this.lblImage, pnlText);
        layout.linkSize(0, this.lblImage, this.btnClose);
        this.getContentPane().add(this.panel);
        this.pack();
        this.setLocationRelativeTo(this.owner);
        this.setIconImage(GDImagePool.iconLogo64x64.getImage());
    }

    private JPanel buildTextPanel(final String text, final int iconType) {
        final JPanel panel = new JPanel();
        Font fntLabel = UIManager.getDefaults().getFont("Label.font");
        fntLabel = fntLabel.deriveFont((float) GDStashFrame.iniConfig.sectUI.fontSize);
        GroupLayout layout = null;
        GroupLayout.SequentialGroup hGroup = null;
        GroupLayout.SequentialGroup vGroup = null;
        final Border lowered = BorderFactory.createEtchedBorder(1);
        final Border raised = BorderFactory.createEtchedBorder(0);
        final Border compound = BorderFactory.createCompoundBorder(raised, lowered);
        panel.setBorder(compound);
        if (iconType != 0) {
            this.lblIcon = new JLabel();
            this.setIcon(iconType);
            this.lblIcon.setBorder(compound);
        }
        (this.lblText = new JLabel()).setText(text);
        this.lblText.setVerticalAlignment(0);
        this.lblText.setFont(fntLabel);
        layout = new GroupLayout(panel);
        panel.setLayout(layout);
        layout.setAutoCreateGaps(true);
        layout.setAutoCreateContainerGaps(true);
        hGroup = layout.createSequentialGroup();
        if (iconType == 0) {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblText));
        } else {
            hGroup.addGroup(layout.createParallelGroup().addComponent(this.lblIcon)).addGroup(layout.createParallelGroup().addComponent(this.lblText));
        }
        layout.setHorizontalGroup(hGroup);
        vGroup = layout.createSequentialGroup();
        if (iconType == 0) {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblText));
        } else {
            vGroup.addGroup(layout.createParallelGroup().addComponent(this.lblIcon).addComponent(this.lblText));
        }
        layout.setVerticalGroup(vGroup);
        if (iconType != 0) {
            layout.linkSize(1, this.lblIcon, this.lblText);
        }
        return panel;
    }

    private void setIcon(final int iconType) {
        switch (iconType) {
            case 1: {
                this.lblIcon.setIcon(GDImagePool.iconMsgSuccess32);
                break;
            }
            case 2: {
                this.lblIcon.setIcon(GDImagePool.iconMsgInfo32);
                break;
            }
            case 3: {
                this.lblIcon.setIcon(GDImagePool.iconMsgWarning32);
                break;
            }
            case 4: {
                this.lblIcon.setIcon(GDImagePool.iconMsgError32);
                break;
            }
            default: {
                this.lblIcon.setIcon(null);
                break;
            }
        }
    }

    private class CloseActionListener implements ActionListener {
        @Override
        public void actionPerformed(final ActionEvent e) {
            GDLogoDialog.this.setVisible(false);
            GDLogoDialog.this.dispatchEvent(new WindowEvent(GDLogoDialog.this, 201));
        }
    }
}
